{% extends "base.html" %}
{% block page_title %}题目列表 - {% endblock %}
{% block page_head %}
    <link rel="stylesheet" href="/static/library/jstree/themes/default/style.min.css">
    <script type="text/javascript" src="/static/library/jstree/jstree.min.js"></script>
    <style type="text/css">
    .problem_items{
        cursor: pointer;
        cursor: hand;
    }
    .problem_detail_panel{
        display: none;
    }
    .problem_detail_panel_active{
        display: table-row !important;
    }
    </style>
    <script type="text/javascript">
    $(function () {
        $(".problem_items").dblclick(function () {
            var id = $(this).attr("data-id");
            $("#problem_detail_area_"+id).html("预览加载中...");
            $.GRestP({
                responseType: "json",
                callback: function (rel, entity) {
                    if(rel){
                        $("#problem_detail_area_"+id).html(entity.msg);
                    }else{
                        $("#problem_detail_area_"+id).html("加载失败，请重试。");
                    }
                }
            }).call('{% url 'problem_get_problem_preview' 0 %}'.replace("problem/0", "problem/"+id));
            $(this).next(".problem_detail_panel").toggleClass("problem_detail_panel_active")
        });
    })
    </script>
{% endblock %}
{% block page_body %}
<div class="header_container">
    <h1>题目列表
    {% if asgn_id != None and asgn_id != "" and user_session.user_role >= 2 %}
        <small>/ 作业选题模式 - 目标作业：{{ asgn_choosing.name }}({{ asgn_id }}) - <a href="javascript:window.close();">[退出]</a></small>
    {% endif %}
    </h1>
</div>

<div class="page_container">
    <!--toolbar-->
    {% include "problem/archive/filter.html" %}
    <div class="row">
        <div class="col-sm-12">
            {% if user_session.user_role >= 2 %}
                {% if asgn_id == "" %}
                <ul class="pagination">
                    <li><a href="{% url 'problem_mgr_new_problem' %}" title="添加新题目"><span class="glyphicon glyphicon-plus"></span>添加题目</a></li>
                    <li id="btn-bm-visiable" class="disabled"><a href="javascript:void(0)" title="批量设置可见"><span class="glyphicon glyphicon-eye-open">可见</span></a></li>
                    <li id="btn-bm-invisiable" class="disabled"><a href="javascript:void(0)" title="批量设置不可见"><span class="glyphicon glyphicon-eye-close">不可见</span></a></li>
                </ul>
                <ul class="pagination">
                    <li id="btn-bm-addclass" class="disabled"><a href="javascript:void(0)" title="批量添加分类"><span class="glyphicon glyphicon-plus-sign">移入分类</span></a></li>
                    <li id="btn-bm-removeclass" class="disabled"><a href="javascript:void(0)" title="批量从当前分类移出"><span class="glyphicon glyphicon-minus-sign">移出分类</span></a></li>
                </ul>
                {% else %}
                <ul class="pagination">
                    <li class="active" id="btn-asgn-problem"><a href="javascript:void(0)"  style="cursor:pointer;" title="添加题目到作业"><span class="glyphicon glyphicon-plus"></span>添加题目到作业</a>
                </li>
                </ul>
                {% endif %}
            {% endif %}
            <ul class="pagination">
                <li><a href="javascript:WeJudge.problem_jumper('{% url 'problem_show' 0 %}')" title="转到题目"><span class="glyphicon glyphicon-console"></span>&nbsp;转到题目</a></li>
            </ul>
            {{ pager }}
            {% include "problem/archive/list_body.html" %}
             {{ pager }}
        </div>
    </div>


</div>
{% endblock %}
{% block page_script %}

<script type="text/javascript">
    $(function () {

        $(".filter-diff").click(function(){
            $(".filter-diff").removeClass("label-primary").removeClass('label-transparent');
            $(this).addClass("label-primary");
            $(".filter-diff").not(".label-primary").addClass('label-transparent');
            $("#ipt-filter-diff").val($(this).attr("dat-val"));
        });
        $(".filter-order").click(function(){
            $(".filter-order").removeClass("label-primary").removeClass('label-transparent');
            $(this).addClass("label-primary");
            $(".filter-order").not(".label-primary").addClass('label-transparent');
            $("#ipt-filter-order").val($(this).attr("dat-val"));
        });
        $("#problem_filter_bar").click(function(){
            $("#problem_filter_form").slideToggle();
        });
        $("#problem_filter_open_more").click(function(){
            $("#problem_filter_form").slideDown();
            $(this).hide();
        });

        var $bm_form = $("#batch-manage-form");
        var classify_id = {{ classify_id }};
        $bm_form.find(".batch-manage-checkbox").change(function () {
           if($bm_form.find(".batch-manage-checkbox:checked").length > 0){
               $("#btn-bm-visiable, #btn-bm-invisiable, #btn-bm-addclass, #btn-bm-removeclass").removeClass("disabled");
           }else{
               $("#btn-bm-visiable, #btn-bm-invisiable, #btn-bm-addclass, #btn-bm-removeclass").addClass("disabled");
           }
        });
        $bm_form.find("#select-all").change(function () {
            $bm_form.find(".batch-manage-checkbox").prop("checked", $(this).prop("checked")).change();
        });
        {% if asgn_id == "" %}
        $("#btn-bm-invisiable").click(function () {
            if($(this).hasClass("disabled")) return false;
            $.GRestP({
                responseType: "json",
                callback: function (flag, entity) {
                    if(flag){
                        $.AlertBox({body: entity.msg, color: "success", title: "操作成功", callback: function(){window.location.reload();} }).show();
                    }else{
                        $.AlertBox({body: entity.msg, color: "danger", title: "操作失败" }).show();
                    }
                }
            }).submit_form("#batch-manage-form",'{% url 'problem_mgr_batch_change_visiable' 0 %}');
        });
        $("#btn-bm-visiable").click(function () {
            if($(this).hasClass("disabled")) return false;
            $.GRestP({
                responseType: "json",
                callback: function (flag, entity) {
                    if(flag){
                        $.AlertBox({body: entity.msg, color: "success", title: "操作成功", callback: function(){window.location.reload();} }).show();
                    }else{
                        $.AlertBox({body: entity.msg, color: "danger", title: "操作失败" }).show();
                    }
                }
            }).submit_form("#batch-manage-form",'{% url 'problem_mgr_batch_change_visiable' 1 %}');
        });
        $("#btn-bm-addclass").click(function () {
            if($(this).hasClass("disabled")) return false;
            $.ModalBox({
                title: '移入分类',
                mode: "url",
                size: "mid",
                url: "{% url 'problem_classify_selector' %}",
                default_btn_action: function () {
                    $("#batch-manage-form").unbind("submit").submit(function () {
                        var cid = $("#problem_classify_selector_id").val();
                        $.GRestP({
                            responseType: "json",
                            callback: function (flag, entity) {
                                if (flag) {
                                    $.AlertBox({
                                        body: entity.msg, color: "success", title: "操作成功", callback: function () {
                                            $.ModalBox({}).hide();
                                        }
                                    }).show();
                                } else {
                                    $.AlertBox({body: entity.msg, color: "danger", title: "操作失败"}).show();
                                }
                            }
                        }).submit_form("#batch-manage-form",'{% url 'problem_mgr_bm_add_classify_to_problem' 0 %}'.replace("0", cid));
                        return false;
                    }).submit();
                }
            }).show();
        });
        $("#btn-bm-removeclass").click(function () {
             if($(this).hasClass("disabled")) return false;
            {% if classify_id > 0 %}
             $.ConfirmBox({
                title: "操作提示",
                body: "将从当前分类ID为{{ classify_id }}中移除相应题目，是否继续？",
                color: 'warning',
                callback: function (flag) {
                    $("#batch-manage-form").unbind("submit").submit(function () {
                        var cid = $("#problem_classify_selector_id").val();
                        $.GRestP({
                            responseType: "json",
                            callback: function (flag, entity) {
                                if (flag) {
                                    $.AlertBox({
                                        body: entity.msg, color: "success", title: "操作成功", callback: function () {
                                            $.ModalBox({}).hide();
                                            window.location.reload();
                                        }
                                    }).show();
                                } else {
                                    $.AlertBox({body: entity.msg, color: "danger", title: "操作失败"}).show();
                                }
                            }
                        }).submit_form("#batch-manage-form",'{% url 'problem_mgr_bm_remove_classify_from_problem' classify_id %}');
                        return false;
                    }).submit();
                }
            }).show();
            {% else %}
                $.AlertBox({body: "不能在根分类执行此操作，请先通过筛选器进入到子分类下的题目列表", color: "danger", title: "操作失败"}).show();
            {% endif %}
        });
        {% else %}
        $("#btn-asgn-problem").click(function () {
            if($(this).hasClass("disabled")) return false;
            $.GRestP({
                responseType: "json",
                callback: function (flag, entity) {
                    console.log(flag);
                    if(flag){
                        $.AlertBox({body: entity.msg, color: "success", title: "操作成功", callback: function(){
                            var plist_url = '{% url 'asgn_problem_list' asgn_id %}';
                            if (window.opener.location.href.indexOf(plist_url) > -1){
                                window.opener.location.reload();
                            }else{
                                window.opener.location.href = plist_url;
                            }
                            window.close();
                        }
                    }).show();
                    }else{
                        $.AlertBox({body: entity.msg, color: "danger", title: "操作失败" }).show();
                    }
                }
            }).submit_form("#batch-manage-form",'{% url 'asgn_mgr_add_problems' asgn_id %}');
        });
        {% endif %}
    });
</script>
{% endblock %}